package com.munisense.webservices.javaclient.examples.basicexample;

import java.io.IOException;

import javax.xml.bind.UnmarshalException;

import com.munisense.webservices.javaclient.*;
import com.munisense.webservices.javaclient.exceptions.MuniWebserviceException;
import com.munisense.webservices.pojo.DOStreetLight;
import com.munisense.webservices.pojo.DOStreetLightList;
import org.apache.commons.configuration.*;

public class DOStreetLightSample {

	public static void main(String[] args) {

		// Load service configuration based on configuration file
		ServiceConf serviceConf = configureService("src/main/resources/config.xml");

		// If service configuration was successful
		if (serviceConf != null) {

			try {
				// Instantiate a StreetLight Service
				DOStreetLightService service = new DOStreetLightService(serviceConf);

				// Fetch all the StreetLight objects
				DOStreetLightList list = service.getList(null, null);
				Integer streetLightId = null;
				// Present the output
				if (list != null) {
					System.out.println("Portal " + serviceConf.getHost() + " has " + list.getDOStreetLight().size() + " DOStreetLights: ");

					// Loop over every StreetLight in the list
					for (DOStreetLight light : list.getDOStreetLight()) {

						// Display the description and level of the streetlight
						System.out.println(" * " + light.getDescription().getValue() + " - " + light.getLevel().getValue() + "%");

						streetLightId = Integer.valueOf(light.getId());
					}
				}

				// If there are streetlights
				if (streetLightId != null) {
					// Fetch a single DOStreetLight
					DOStreetLight streetlight = service.get(streetLightId, null);

					// Generate a new level
					String newLevel = String.valueOf(Math.floor(Math.random()*5));
					
					System.out.println(" * " + streetlight.getDescription().getValue() + " - Setting level to " + newLevel + "%");
					
					// Set the level
					streetlight.getLevel().setValue(newLevel);
					
					// Commit the change
					service.update(streetlight);
				}

				// Catch errors
			} catch (UnmarshalException e) {
				System.out.println("There was a problem with processing the server response: ");
				e.printStackTrace();
			} catch (MuniWebserviceException e) {
				System.out.println("The server responded with an error: ["+e.getStatusCode()+"] "+e.getMessage());
				e.printStackTrace();
			}catch (IOException e) {
				System.out.println("An unidentified error situation occured: ");
				e.printStackTrace();
			} catch (Exception e) {
				System.out.println("An unidentified error situation occured: ");
				e.printStackTrace();
			}

		}
	}

	/**
	 * Fills the service configuration object with data from the configuration
	 * file
	 * 
	 * @param configFile
	 * @return ServiceConf|null
	 */
	private static ServiceConf configureService(String configFile) {
		ServiceConf serviceConf = null;

		try {
			Configuration config = new XMLConfiguration("config.xml");
			serviceConf = new ServiceConf(config.getString("host"), config.getString("username"), config.getString("password"), Boolean.parseBoolean(config.getString("allowUnverifiedHost")), Boolean.parseBoolean(config.getString("enableLogging")));
		} catch (ConfigurationException e1) {
			e1.printStackTrace();
		}

		return serviceConf;
	}
}
